package com.iman.Confucius.service.impl;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.iman.Confucius.domain.SessionBean;
import com.iman.Confucius.domain.User;
import com.iman.Confucius.repository.IRedisGo;
import com.iman.Confucius.service.SharedSessionService;
import com.iman.common.util.DateUtils;
import com.iman.common.util.EncryptTool;
import com.iman.common.util.JsonMapper;
import com.iman.common.util.MD5;



/**
 * 保存共享session
 * @author Thinkpad
 */

@Service("shareusersession")
public class SharedSessionServiceImpl implements SharedSessionService{

    private  Logger logger = LoggerFactory.getLogger(SharedSessionServiceImpl.class);
    
	  
    @Inject
    IRedisGo redis;

 
    
    private String  getUserKey(Long  userId){
    	//构造对应的  sessionkey
		try {
			 return MD5.digest( EncryptTool.sha1HEX(userId.toString())  );
		} catch (Exception e) {
			logger.error(" 计算session key的时候出错",e );
		}
		return null;
    }
	
	/**
	 * 放用户session
	 */
	@Override
	public String putSessionUser(User user) {
		assert(user!=null);
		
		
		SessionBean session = new SessionBean();
		//设置登陆时间
		session.setLoginTime( DateUtils.getCurrentDate().getTime());
		session.setUser(user);
		String sessionkey = null;
		//构造对应的  sessionkey
		 
		 sessionkey =getUserKey(user.getUserId());
			 
		 assert(sessionkey!=null);
		 
		//将用户信息 编程 字符串保存
		String strSession = JsonMapper.toJson(session);
		this.redis.putMap(SessionBean.USER_SESSION_KEY_PERFIX+sessionkey , SessionBean.USER_SESSION_SUBKEY_SESSION, strSession);
		 //设置超时时间
		this.redis.setExiperse(sessionkey , session.getSaveTempTimes());
		
		logger.info(" 增加session内容  >>>"+strSession +" userid=:"+user.getUserId()+" set exiperse:="+session.getSaveTempTimes() +"s" ); 
		
		return sessionkey;
	}

	/**
	 * 存放session
	 */
	@Override
	public String putSession(SessionBean sessionbean) {
		assert(sessionbean!=null);
		assert(sessionbean.getUser()!=null);
		
		//设置登陆时间
		sessionbean.setLoginTime( DateUtils.getCurrentDate().getTime());
		 
		String sessionkey = null;
		//构造对应的  sessionkey
		 sessionkey =getUserKey(sessionbean.getUser().getUserId());
		 
		 assert(sessionkey!=null);
		 
		//将用户信息 编程 字符串保存
		String strSession = JsonMapper.toJson(sessionbean);
		this.redis.putMap(SessionBean.USER_SESSION_KEY_PERFIX+sessionkey , SessionBean.USER_SESSION_SUBKEY_SESSION, strSession);
		 //设置超时时间
		this.redis.setExiperse(sessionkey , sessionbean.getSaveTempTimes());
		
		logger.info(" 增加session内容  >>>"+strSession +" userid=:"+sessionbean.getUser().getUserId()+" set exiperse:="+sessionbean.getSaveTempTimes() +"s" ); 
		
		return sessionkey;
	}

	/**
	 * 返回null 没有session  失效
	 */
	@Override
	public SessionBean getSeesion(String sessionkey) {
		 String seesionvalue =  this.redis.getMap( SessionBean.USER_SESSION_KEY_PERFIX+sessionkey,  SessionBean.USER_SESSION_SUBKEY_SESSION);
		 if(seesionvalue!=null && seesionvalue.length()>1){
			 SessionBean session = JsonMapper.toObject(seesionvalue , SessionBean.class);
			 return session;
		 }
		 return null;
	}

	@Override
	public User getSessionUser(String sessionkey) {
		 String seesionvalue =  this.redis.getMap( SessionBean.USER_SESSION_KEY_PERFIX+sessionkey,  SessionBean.USER_SESSION_SUBKEY_SESSION);
		 if(seesionvalue!=null && seesionvalue.length()>1){
			 SessionBean session = JsonMapper.toObject(seesionvalue , SessionBean.class);
			if(session!=null){
				return session.getUser();
			}
		 }
		  
		return null;
	}

	 /*
	  * 通过用户ID 获取 session 信息
	  * (non-Javadoc)
	  * @see com.iman.manjusri.service.SharedSessionService#getSeesion(java.lang.Long)
	  */
	@Override
	public SessionBean getSeesion(Long userid) {
		String sessionkey =getUserKey(userid);
		return getSeesion(sessionkey);
	}

	/*
	 * 只获取 用户信息
	 * (non-Javadoc)
	 * @see com.iman.manjusri.service.SharedSessionService#getSessionUser(java.lang.Long)
	 */
	@Override
	public User getSessionUser(Long userid) {
		String sessionkey =getUserKey(userid);
		return getSessionUser(sessionkey);
	}

	
	/**
	 * 设置session key
	 */
	@Override
	public String addSessionSaveTimes(String sessionKey, int timesSeconds) {
		this.redis.setExiperse(sessionKey , timesSeconds);
		return null;
	}
	
	/*
	 * 设置userID  返回  sessionkey
	 * (non-Javadoc)
	 * @see com.iman.manjusri.service.SharedSessionService#addSessionSaveTimes(java.lang.Long, int)
	 */
	@Override
	public String addSessionSaveTimes(Long userid, int timesSeconds) {
		String sessionkey =getUserKey(userid);
		return addSessionSaveTimes(sessionkey,timesSeconds);
	}

}
